﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xbd.ControlLib;

namespace MYZNMarking
{
    public partial class FrmInfo : Form
    {

        public static Action<bool[]>  CAction { get; set; }

        public FrmInfo()
        {
            InitializeComponent();
            this.list_Info.Columns[1].Width = this.list_Info.Width = this.list_Info.Columns[0].Width - 25;
            FrmMain.InAction = new Action<bool[]>((data1) =>
            {
                Invoke(new Action(() =>
                {
                    foreach (var item in this.groupBox2.Controls.OfType<xbdLed>())
                    {
                        if (item.Tag != null && item.Tag.ToString().Length > 0)
                        {
                            int index = Convert.ToInt32(item.Tag.ToString());
                            item.LedState = data1[index] ? 1 : 0;
                        }
                    }
                }));
            });
            FrmMain.NumAction = new Action<string[]>((data2) =>
            {
                Invoke(new Action(() =>
                {
                    this.lbl_yield.Text = data2[1] + "PCS";
                    this.TXT_NGNUMBER.Text = data2[2] + "PCS";
                    this.lbl_OK.Text = data2[0];
                    this.lbl_OK.BackColor = data2[0] == "OK" ? Color.Lime : Color.Red;
                
                }));
            });
        }
        private bool[] inbool=new bool[4];
        private void btn_Start_Click(object sender, EventArgs e)
        {
            inbool[0]=true;
            CAction?.Invoke(inbool);
            inbool[0] = false;
        }

        private void btn_cylinderCommon_Click(object sender, EventArgs e)
        {
            if (sender is Button button)
            {
                int index = Convert.ToInt32(button.Tag);
                inbool[index]=!inbool[index];
                button.BackColor=inbool[index]?Color.Lime: Color.Transparent;
                CAction?.Invoke(inbool);
            }
        }
        private string CurrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }
        public void AddLog(int level, string log)
        {
            if (level > 2)
            {
                level = 2;
            }
            if (level < 0)
            {
                level = 0;
            }
            if (this.list_Info.InvokeRequired)
            {
                this.list_Info.Invoke(new Action<int, string>(AddLog), level, log);
            }
            else
            {
                ListViewItem listViewItem = new ListViewItem(CurrentTime, level);
                listViewItem.SubItems.Add(log);
                this.list_Info.Items.Add(listViewItem);
                //让最新的日志显示在最下面
                this.list_Info.Items[this.list_Info.Items.Count - 1].EnsureVisible();//可见
            }

        }
    }
}

